#include "header.h"

static int 		myid, np;
static MPI_Status  	status;
static MPI_Request	request;
static FILE		*fh, *fhlog;
static char		line[80], value[80], param[80];
static int 		flag = 1;

void load_params(int *argc, char ***argv, ctrl_ptr in)
{
  MPI_Comm_size(COMM, &np);
  MPI_Comm_rank(COMM, &myid);
  MPI_Barrier(COMM);  

  if (argc[0] != 2) {
    if (myid == 0) printf("\n\tusage: %s run_name\n\n", argv[0][0]);
    MPI_Finalize();
    exit(1);
  }
  MPI_Barrier(COMM);  
  strcpy(in->runname, argv[0][1]);
  MPI_Barrier(COMM);  
  if (myid == 0)
  {
    sprintf(line, "%s.cfg", in->runname);
    printf("%s\n",line);
    fh    = fopen(line,"r");
    if (fh == NULL) flag = -1;
  }
  MPI_Barrier(COMM);  
  MPI_Bcast(&flag, 1, MPI_INTEGER, 0, COMM);
  MPI_Barrier(COMM);  
  if (flag != 1)
  {
    if (myid == 0) printf("Input file missing\n");
    MPI_Finalize();
    exit(1);
  }
  MPI_Barrier(COMM);  
  if (myid == 0)
  {  
    while (fgets(line, 80, fh) != NULL)
    {
      sscanf(line, "%s\t%s", param, value);
      if (strcmp(param,"gridpoints") == 0) in->n = atoi(value);
      if (strcmp(param,"boxlength") == 0) in->l = atof(value);
      if (strcmp(param,"cfl_condition") == 0) in->cfl = atof(value);
      if (strcmp(param,"amplitude") == 0) in->b = atof(value);
      if (strcmp(param,"ic_width") == 0) in->a = atof(value);
      if (strcmp(param,"final_time") == 0) in->tmax = atof(value);
      if (strcmp(param,"heat_flag") == 0) in->heat = atoi(value);
      if (strcmp(param,"io_interval") == 0) in->tio = atof(value);
      if (strcmp(param,"offset") == 0) in->offset = atof(value);
      if (strcmp(param,"num_particles") == 0) in->Npart = atof(value);
      if (strcmp(param,"delta_2") == 0) in->delta2 = atof(value);
      if (strcmp(param,"method_order") == 0) in->m_order = atoi(value);
      if (strcmp(param,"gravity_flag") == 0) in->gFlag = atoi(value);
      if (strcmp(param,"mask_flag") == 0) in->mFlag = atoi(value);
      if (strcmp(param,"fourier_reg") == 0) in->m = atof(value);
      if (strcmp(param,"restart_name") == 0) strcpy(in->rname, value); 
    }
    fclose(fh);
  }
  MPI_Barrier(COMM);  
  MPI_Bcast(in, sizeof(ctrl_str), MPI_BYTE, 0, COMM);
  MPI_Barrier(COMM);  
  write_log(in);
}
void write_log(ctrl_ptr in)
{
  MPI_Comm_size(COMM, &np);
  MPI_Comm_rank(COMM, &myid);
  MPI_Barrier(COMM);  
  if (myid != 0) MPI_Recv(&flag, 1, INT, myid-1, myid, COMM, &status);
  sprintf(line, "%s.log", in->runname);
  fhlog = fopen(line, "a");
  fprintf(fhlog, "#process %d of %d recieved parameters\n\n", myid+1, np);
  fprintf(fhlog, "n = %td\n", in->n);
  fprintf(fhlog, "l = %f\n", in->l);
  fprintf(fhlog, "cfl = %f\n", in->cfl);
  fprintf(fhlog, "b = %f, amplitude of ic\n", in->b);
  fprintf(fhlog, "a = %f, ic width\n", in->a);
  fprintf(fhlog, "tmax = %f\n", in->tmax);
  fprintf(fhlog, "Nparticles = %f\n", in->Npart);
  fprintf(fhlog, "heat eqn: %td\n",in->heat);
  fprintf(fhlog, "tio = %f, output interval\n", in->tio);
  fprintf(fhlog, "offset = %e, moves grid by offset*dx\n", in->offset);
  fprintf(fhlog, "with %d order method\n", in->m_order);
  fprintf(fhlog, "delta2 = %e, regularisation of potential at origin\n", in->delta2);
  fprintf(fhlog, "Fourier regularization coefficient is %e\n", in->m);
  fprintf(fhlog, "Explicit gravity is %d\n", in->gFlag);
  fprintf(fhlog, "Border mask is %d\n", in->mFlag);
  fprintf(fhlog, "restart_name = %s\n\n", in->rname);
  fclose(fhlog);

  if (myid != np-1) MPI_Send(&flag, 1, INT, myid+1, myid+1, COMM);
  MPI_Barrier(COMM);  

}

